<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>作用域</title>
</head>

<body>
    <script>
        /*
        var scope = "global"
        function checkscope() {
            var scope = "local";
            return scope;
        }
        console.log(checkscope());
        //local
         */

        /*
        var scope = "global";
        function checkscope2() {
            var scope = "local";
            var myscope = "local";
            return (scope, myscope);
        }
        checkscope2();
        console.log(scope, checkscope2());
        //global,['local','local'] */

        /*
        var scope = "global";//声明一个全局变量
        function checkscope2() {
            var scope = "local";//声明一个同名的部变量
            return scope;//local，返回局部变量的值，而不是全局的
        }
        console.log(checkscope2()); */

        /*
        scope = "global";
        function checkscope2() {
            scope = "local";
            //修改的全局变量
            myscope = "local";
            return (scope, myscope);
        }
        checkscope2();
        console.log(scope, checkscope2());
        //全为local
        */

        /*
        var scope = "global scope"
        //全局
        function checkscope() {
            var scope = "local scope"//局部
            function nested() {
                var scope = "nested scope";//嵌套作用域
                return scope;
            }
            return nested();
        }
        checkscope();
        console.log(checkscope());
        //nested scope 嵌套作用域 */

        /*
        var scope = "global";
        function f() {
            var scope;
            console.log(scope);
            //undefined
            scope = "local";
            console.log(scope);
            //local
        }
        f(); 
        */

        /*
        var a = "apple";
        function fighting() {
            var a = "angel";
        }
        console.log(a);//apple

        var b = "apple";
        if (true) {
            var b = "angel";
        }
        console.log(b);//angel
        */

        /*
        var a = "apple";
        var b = "boy";
        function fighting() {
            var a = "angry";
            console.log(a);//angry
            console.log(b);//boy
        }
        fighting();
        console.log(a);//apple
        */

        /*
        var a = "apple";
        function fighting() {
            console.log(a);
            //undefined
            var a = "angry";
            console.log(a);
            //angry
        }
        fighting();
        */

        var a = "apple";
        function fighting() {
            console.log(a);
            //undefined
            var a = "angry";
            console.log(ss());
            //angry
            function ss() {
                return a;
            }
        }
        fighting();
    </script>
</body>

</html>